//
//  ViewController3.swift
//  JKTest
//
//  Created by IronMan on 2020/8/28.
//  Copyright © 2020 Creditease. All rights reserved.
//

import UIKit

class ViewController3: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = "ViewController3"
        self.view.backgroundColor = UIColor.green
        /*
         public enum RoundingMode : UInt {
             // 是四舍五入
             case plain = 0
             // 是向下取整
             case down = 1
             // 是向上取整
             case up = 2
             // 是在四舍五入的基础上，加上末尾数为5时，变成偶数的规则(也是最后一位是 5 的时候，看前一位是不是偶数，是偶数就不变，不是偶数就加1)
             case bankers = 3
         }
   
         NSDecimalNumberHandler 初始化方法
         roundingMode 舍入方式
         scale 小数点后舍入值的位数
         raiseOnExactness 精度错误处理
         raiseOnOverflow 溢出错误处理
         raiseOnUnderflow 下溢错误处理
         raiseOnDivideByZero 除以0的错误处理

        */
        // 加法
        // add()
        // 减法
        // subtracting()
        // 乘法
        // multiplyingBy()
        // 除法
        dividing()
    }
    
    // MARK:- bankers 是在四舍五入的基础上，加上末尾数为5时，变成偶数的规则(也是最后一位是 5 的时候，看前一位是不是偶数，是偶数就不变，不是偶数就加1)
    /// 除法
    func dividing() {
        let amountHandler = NSDecimalNumberHandler(roundingMode: .bankers, scale: 2, raiseOnExactness: false, raiseOnOverflow: false, raiseOnUnderflow: false, raiseOnDivideByZero: false)
        let oneStr = "1.114"
        let twoStr = "1.125"
        let dividingStr = "1.0"
        
        let oneNum = NSDecimalNumber(string: oneStr)
        let twoNum = NSDecimalNumber(string: twoStr)
        let dividingNum = NSDecimalNumber(string: dividingStr)

        // 加法
        let result1 = oneNum.dividing(by: dividingNum, withBehavior: amountHandler)
        let result2 = twoNum.dividing(by: dividingNum, withBehavior: amountHandler)
        
        print("\n是在四舍五入的基础上，加上末尾数为5时，变成偶数的规则(也是最后一位是 5 的时候，看前一位是不是偶数，是偶数就不变，不是偶数就加1)\n\(oneStr) / \(dividingStr) （保留两位小数）向下取整的结果为:\(result1)\n\(twoStr) / \(dividingStr)（保留两位小数）向下取整的结果为:\(result2)")
    }
    
    // MARK:- up 向上取整
    /// 乘法
    func multiplyingBy() {
        let amountHandler = NSDecimalNumberHandler(roundingMode: .up, scale: 2, raiseOnExactness: false, raiseOnOverflow: false, raiseOnUnderflow: false, raiseOnDivideByZero: false)
        let oneStr = "1.114"
        let twoStr = "1.116"
        let multiplyingByStr = "1.0"
        
        let oneNum = NSDecimalNumber(string: oneStr)
        let twoNum = NSDecimalNumber(string: twoStr)
        let multiplyingByNum = NSDecimalNumber(string: multiplyingByStr)

        // 加法
        let result1 = oneNum.multiplying(by: multiplyingByNum, withBehavior: amountHandler)
        let result2 = twoNum.subtracting(multiplyingByNum, withBehavior: amountHandler)
        
        print("\n向上取整\n\(oneStr) * \(multiplyingByStr) （保留两位小数）向下取整的结果为:\(result1)\n\(twoStr) * \(multiplyingByStr)（保留两位小数）向下取整的结果为:\(result2)")
    }
    
    // MARK:- down 向下取整
    /// 减法
    func subtracting() {
        let amountHandler = NSDecimalNumberHandler(roundingMode: .down, scale: 2, raiseOnExactness: false, raiseOnOverflow: false, raiseOnUnderflow: false, raiseOnDivideByZero: false)
        let oneStr = "1.114"
        let twoStr = "1.116"
        let subtractingStr = "1.0"
        
        let oneNum = NSDecimalNumber(string: oneStr)
        let twoNum = NSDecimalNumber(string: twoStr)
        let subtractingNum = NSDecimalNumber(string: subtractingStr)

        // 加法
        let result1 = oneNum.subtracting(subtractingNum, withBehavior: amountHandler)
        let result2 = twoNum.subtracting(subtractingNum, withBehavior: amountHandler)
        
        print("\n向下取整\n\(oneStr) - \(subtractingStr) （保留两位小数）向下取整的结果为:\(result1)\n\(twoStr) - \(subtractingStr)（保留两位小数）向下取整的结果为:\(result2)")
    }
    
    // MARK:- plain 四舍五入
    /// 加法
    func add() {
        let amountHandler = NSDecimalNumberHandler(roundingMode: .plain, scale: 2, raiseOnExactness: false, raiseOnOverflow: false, raiseOnUnderflow: false, raiseOnDivideByZero: false)
        let oneStr = "1.114"
        let twoStr = "1.116"
        let addStr = "1.0"
        
        let oneNum = NSDecimalNumber(string: oneStr)
        let twoNum = NSDecimalNumber(string: twoStr)
        let addNum = NSDecimalNumber(string: addStr)

        // 加法
        let result1 = oneNum.adding(addNum, withBehavior: amountHandler)
        let result2 = twoNum.adding(addNum, withBehavior: amountHandler)
        
        print("\n四舍五入\n\(oneStr) + \(addStr) （保留两位小数）四舍五入的结果为:\(result1)\n\(twoStr) + \(addStr) （保留两位小数）四舍五入的结果为:\(result2)")
    }
    
    


}
